#include <algorithm>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

class Solution {
public:
    vector<int> peopleIndexes(vector<vector<string>>& favoriteCompanies) {
        unordered_map<string, int> word_id;
        vector<pair<vector<string>*, int>> favorite_companies(favoriteCompanies.size());
        for (int i = 0; i < favoriteCompanies.size(); i++) {
            favorite_companies[i] = {&favoriteCompanies[i], i};
            for (string& it : favoriteCompanies[i]) {
                if (word_id.find(it) == word_id.end()) {
                    word_id[it] = word_id.size();
                }
            }
        }
        sort(favorite_companies.begin(), favorite_companies.end(), [] (pair<vector<string>*, int>& a, pair<vector<string>*, int>& b) {
            if (a.first->size() == b.first->size()) {
                return a.second < b.second;
            } else {
                return a.first->size() > b.first->size();
            }
        });
        vector<pair<set<int>, int>> favorite_companies_arr(favoriteCompanies.size());
        for (int i = 0; i < favorite_companies_arr.size(); i++) {
            set<int> set;
            for (string& str : *favorite_companies[i].first) {
                set.insert(word_id[str]);
            }
            favorite_companies_arr[i] = {set, favorite_companies[i].second};
        }
        vector<int> ans;
        for (int i = 0; i < favorite_companies_arr.size(); i++) {
            if (favorite_companies_arr[i].first.empty()) {
                continue;
            }
            for (int j = i + 1; j < favorite_companies_arr.size(); j++) {
                if (includes(favorite_companies_arr[i].first.begin(), favorite_companies_arr[i].first.end(), favorite_companies_arr[j].first.begin(), favorite_companies_arr[j].first.end())) {
                    favorite_companies_arr[j].first.clear();
                }
            }
            ans.emplace_back(favorite_companies_arr[i].second);
        }
        sort(ans.begin(), ans.end());
        return ans;
    }
};